﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class Administrar_CVs
    Inherits System.Web.UI.Page
    Private _id_cv As Integer
    Protected Sub gvPersonas_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvPersonas.PageIndexChanging
        gvPersonas.PageIndex = e.NewPageIndex
        cargarGrillaPersonasyEmpleados()
    End Sub

    Protected Sub gvPersonas_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvPersonas.RowDeleting
        Try
            PersonaDB.EliminarPersona(Convert.ToInt32(gvPersonas.DataKeys(e.RowIndex).Values(0).ToString()), gvPersonas.DataKeys(e.RowIndex).Values(1).ToString())
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        LimpiarPersona()
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        cargarGrillaPersonasyEmpleados()
    End Sub

    Protected Sub gvPersonas_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvPersonas.SelectedIndexChanged
        ddlTipoDocumento.SelectedValue = gvPersonas.SelectedDataKey.Values(0).ToString()
        txtApellido.Text = gvPersonas.SelectedRow.Cells(0).Text.ToString()
        txtNombre.Text = gvPersonas.SelectedRow.Cells(1).Text.ToString()
        txtNroDocumento.Text = gvPersonas.SelectedDataKey.Values(1).ToString()
        txtFechaNacimiento.Text = gvPersonas.SelectedRow.Cells(4).Text.ToString()
        txtemail.Text = gvPersonas.SelectedRow.Cells(5).Text.ToString()

        If gvPersonas.SelectedDataKey.Values(2).ToString() = "" Then 'si una persona tiene null es porque no tiene un CV cargado
            _id_cv = 0
        Else
            _id_cv = gvPersonas.SelectedDataKey.Values(2).ToString()
        End If


        cargarGrillaDireccionesPersona()
        cargarGrillaExperiencias()
        cargarGrillaEstudios()
        btnAgregarEducacion.Enabled = True
        btnAgregarExperiencia.Enabled = True
        btnAceptar.Enabled = True
        btnNuevo.Enabled = False

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            CargarDataListTiposDocumento()
            cargarGrillaPersonasyEmpleados()
            CargarDataListCargosExperiencia()
            CargarDataListEstudios()

            If Session("PaginaOrigen") = "DireccionesPersonas" Then
                Dim dr As SqlDataReader
                txtNroDocumento.Text = Session("Nro_documento")
                ddlTipoDocumento.SelectedValue = Convert.ToInt32(Session("id_tipo_documento").ToString)
                dr = PersonaDB.GetPersonaByDocumento(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text)

                While dr.Read()
                    txtNombre.Text = dr("nombre").ToString()
                    txtApellido.Text = dr("apellido").ToString()
                    txtemail.Text = dr("email").ToString()
                    txtFechaNacimiento.Text = dr("fecha_nacimiento")
                End While
                PersonaDB.CerrarConexion()
                habilitarTextBoxes()
                btnNuevo.Enabled = False
            End If

            If txtNroDocumento.Text <> "" Then
                cargarGrillaDireccionesPersona()
            Else
                gvDireccionesPersonas.DataSource = Nothing
                gvDireccionesPersonas.DataBind()
            End If
        End If

        If ddlCargosExperiencia.SelectedValue = 0 Then
            lblOtroCargo.Visible = True
            txtOtroCargo.Visible = True
        Else
            lblOtroCargo.Visible = False
            txtOtroCargo.Visible = False
        End If
    End Sub
    Sub cargarGrillaDireccionesPersona()
        gvDireccionesPersonas.DataSource = PersonaDB.GetDireccionesPersona(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text)
        gvDireccionesPersonas.DataBind()
    End Sub

    Sub cargarGrillaExperiencias()
        gvExperiencia.DataSource = cvDB.GetExperienciasCV(_id_cv)
        gvExperiencia.DataBind()
    End Sub

    Sub cargarGrillaEstudios()
        gvEducacion.DataSource = cvDB.GetEstudiosCV(_id_cv)
        gvEducacion.DataBind()
    End Sub

    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        btnNuevo.Enabled = False
        btnAceptar.Enabled = True
        gvPersonas.Enabled = False
        btnCancelar.Enabled = True
        btnAgregarEducacion.Enabled = False
        btnAgregarExperiencia.Enabled = False
        LimpiarPersona()
        gvEducacion.DataSource = Nothing
        gvEducacion.DataBind()
        gvExperiencia.DataSource = Nothing
        gvExperiencia.DataBind()
    End Sub
    Private Sub LimpiarPersona()
        txtApellido.Text = ""
        txtemail.Text = ""
        txtFechaNacimiento.Text = ""
        txtNombre.Text = ""
        txtNroDocumento.Text = ""
        ddlTipoDocumento.SelectedIndex = -1
        gvPersonas.SelectedIndex = -1
        gvDireccionesPersonas.DataSource = Nothing
        gvDireccionesPersonas.DataBind()

    End Sub

    Protected Sub btnNuevaDireccion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevaDireccion.Click
        Try
            PersonaDB.InsertarPersona(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text, txtNombre.Text, txtApellido.Text, txtFechaNacimiento.Text, txtemail.Text)
            Session("Nro_Documento") = txtNroDocumento.Text
            Session("Id_tipo_documento") = ddlTipoDocumento.SelectedValue
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("DireccionesPersonas.aspx")
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        LimpiarPersona()
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        gvPersonas.Enabled = True

    End Sub

    Protected Sub btnAceptar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Try
            If btnNuevo.Enabled = False Then 'si esta deshabilitado es un nueva persona
                PersonaDB.InsertarPersona(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text, txtNombre.Text, txtApellido.Text, txtFechaNacimiento.Text, txtemail.Text)
            Else 'sino es un update
                PersonaDB.ActualizarPersona(txtNombre.Text, txtApellido.Text, txtFechaNacimiento.Text, txtNroDocumento.Text, ddlTipoDocumento.SelectedValue, txtemail.Text)
            End If

            cargarGrillaPersonasyEmpleados()
            LimpiarPersona()
            btnAceptar.Enabled = False
            btnNuevo.Enabled = True

        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try
    End Sub

    Sub cargarGrillaPersonasyEmpleados()
        gvPersonas.DataSource = PersonaDB.GetEmpleadosyPersonas
        gvPersonas.DataBind()
    End Sub

    Sub cargarGrillaPersonasyEmpleadosByApellido()
        gvPersonas.DataSource = PersonaDB.GetEmpleadosyPersonasByApellido(txtApellidoBusqueda.Text)
        gvPersonas.DataBind()
    End Sub

    Sub habilitarTextBoxes()
        txtApellido.ReadOnly = False
        txtemail.ReadOnly = False
        txtFechaNacimiento.ReadOnly = False
        txtNombre.ReadOnly = False
        txtNroDocumento.ReadOnly = False
        btnNuevaDireccion.Enabled = True

    End Sub
    Sub CargarDataListTiposDocumento()
        Dim ds As New DataSet
        ds = TipoDocumentoDB.GetTiposDocumento
        With ddlTipoDocumento
            .DataSource = ds.Tables("TiposDocumentos")
            .DataValueField = "id_tipo_documento"
            .DataTextField = "tipo_documento"
            .DataBind()
        End With

    End Sub

    Protected Sub btnBuscarApellido_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscarApellido.Click
        cargarGrillaPersonasyEmpleadosByApellido()
        txtApellidoBusqueda.Text = ""
    End Sub

    Protected Sub btnAgregarExperiencia_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregarExperiencia.Click
        Try
            Dim id_experiencia As Integer
            _id_cv = cvDB.InsertarCV(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text, "")

            If ddlCargosExperiencia.SelectedValue = 0 Then
                id_experiencia = ExperienciaDB.InsertarExperiencia(txtOtroCargo.Text)
            Else
                id_experiencia = ddlCargosExperiencia.SelectedValue
            End If

            cvDB.InsertarItemCV(_id_cv, 0, "", "", "", ddlCargosExperiencia.SelectedValue, txtEmpresa.Text, txtDesde.Text, txtHasta.Text)
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try
        
        cargarGrillaExperiencias()
        LimpiarExperiencia()
    End Sub

    Sub CargarDataListCargosExperiencia()
        Dim ds As New DataSet
        ds = ExperienciaDB.GetCargosExperiencias
        With ddlCargosExperiencia
            .DataSource = ds.Tables("Experiencias")
            .DataValueField = "id_experiencia"
            .DataTextField = "experiencia"
            .DataBind()
            .Items.Insert(0, New ListItem("Otro", "0"))
        End With

    End Sub
    Sub CargarDataListEstudios()
        Dim ds As New DataSet
        ds = EstudiosDB.GetEstudios
        With ddlEstudios
            .DataSource = ds.Tables("Estudios")
            .DataValueField = "id_estudio"
            .DataTextField = "estudio"
            .DataBind()
            .Items.Insert(0, New ListItem("Otro", "0"))
        End With

    End Sub

    Protected Sub gvExperiencia_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvExperiencia.PageIndexChanging
        gvExperiencia.PageIndex = e.NewPageIndex
        cargarGrillaExperiencias()
    End Sub
    Private Sub LimpiarExperiencia()
        txtDesde.Text = ""
        txtHasta.Text = ""
        txtEmpresa.Text = ""
        txtOtroCargo.Text = ""
        CargarDataListCargosExperiencia()
    End Sub
    Private Sub LimpiarEstudios()
        txtInstitucion.Text = ""
        txtFechaEstudio.Text = ""
        txtOtro.Text = ""
        CargarDataListEstudios()
    End Sub

    Protected Sub btnAgregarEducacion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregarEducacion.Click
        Try
            Dim id_estudio As Integer
            Dim descripcion_estudio As String

            _id_cv = cvDB.InsertarCV(ddlTipoDocumento.SelectedValue, txtNroDocumento.Text, "")

            If ddlEstudios.SelectedValue = 0 Then
                id_estudio = EstudiosDB.InsertarEstudio(txtOtro.Text)
                descripcion_estudio = txtOtro.Text
            Else
                id_estudio = ddlEstudios.SelectedValue
                descripcion_estudio = ""
            End If

            cvDB.InsertarItemCV(_id_cv, id_estudio, descripcion_estudio, txtInstitucion.Text, txtFechaEstudio.Text, 0, "", "", "")
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        cargarGrillaEstudios()
        LimpiarEstudios()
    End Sub

    Protected Sub gvEducacion_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEducacion.PageIndexChanging
        gvEducacion.PageIndex = e.NewPageIndex
        cargarGrillaEstudios()
    End Sub

    Protected Sub gvExperiencia_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvExperiencia.RowDeleting
        Try
            _id_cv = gvExperiencia.DataKeys(e.RowIndex).Values(0).ToString
            ExperienciaDB.DeleteExperienciaCV(gvExperiencia.DataKeys(e.RowIndex).Values(0).ToString, gvExperiencia.DataKeys(e.RowIndex).Values(1).ToString)
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try
        cargarGrillaExperiencias()
        LimpiarExperiencia()
        
    End Sub

    Protected Sub gvExperiencia_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvExperiencia.SelectedIndexChanged

    End Sub

    Protected Sub gvEducacion_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvEducacion.RowDeleting
        Try
            _id_cv = gvEducacion.DataKeys(e.RowIndex).Values(0).ToString
            EstudiosDB.DeleteEstudioCV(_id_cv, gvEducacion.DataKeys(e.RowIndex).Values(1).ToString)
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Administrar CVs.aspx"
            Server.Transfer("Errores.aspx")
        End Try
        cargarGrillaEstudios()
        LimpiarEstudios()
    End Sub

    Protected Sub gvEducacion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvEducacion.SelectedIndexChanged

    End Sub

    Protected Sub ddlCargosExperiencia_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCargosExperiencia.SelectedIndexChanged

    End Sub
End Class
